package com.gnerv.rabbitmq.agent.command;

import com.gnerv.rabbitmq.agent.model.entity.Node;
import com.gnerv.rabbitmq.agent.tools.ExecTools;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
public class PermissionCommand extends AbstractRabbitmqctlCommand {

    public PermissionCommand(Node node) {
        super();
        setNode(node);
    }

    public static PermissionCommand build(Node node){
        return new PermissionCommand(node);
    }

    public ExecTools.ExecResult setPermissions(String vhost, String username, String conf, String write, String read) {
        List<String> setPermissions = Lists.newArrayList();
        setPermissions.add("set_permissions");
        setPermissions.add("-p");
        setPermissions.add(vhost);
        setPermissions.add(username);
        setPermissions.add(conf);
        setPermissions.add(write);
        setPermissions.add(read);
        return run(setPermissions);
    }

    public ExecTools.ExecResult setTopicPermissions(String vhost, String username, String exchange, String conf, String write, String read) {
        List<String> setTopicPermissions = Lists.newArrayList();
        setTopicPermissions.add("set_topic_permissions");
        setTopicPermissions.add("-p");
        setTopicPermissions.add(vhost);
        setTopicPermissions.add(username);
        setTopicPermissions.add(exchange);
        setTopicPermissions.add(conf);
        setTopicPermissions.add(write);
        setTopicPermissions.add(read);
        return run(setTopicPermissions);
    }

    public ExecTools.ExecResult clearPermissions(String vhost, String username) {
        List<String> clearPermissions = Lists.newArrayList();
        clearPermissions.add("clear_permissions");
        clearPermissions.add("-p");
        clearPermissions.add(vhost);
        clearPermissions.add(username);
        return run(clearPermissions);
    }

    public ExecTools.ExecResult clearTopicPermissions(String vhost, String username, String exchange) {
        List<String> clearTopicPermissions = Lists.newArrayList();
        clearTopicPermissions.add("clear_topic_permissions");
        clearTopicPermissions.add("-p");
        clearTopicPermissions.add(vhost);
        clearTopicPermissions.add(username);
        clearTopicPermissions.add(exchange);
        return run(clearTopicPermissions);
    }

    public ExecTools.ExecResult listPermissions(String vhost) {
        List<String> listPermissions = Lists.newArrayList();
        listPermissions.add("list_permissions");
        listPermissions.add("-p");
        listPermissions.add(vhost);
        return run(listPermissions);
    }

    public ExecTools.ExecResult listTopicPermissions(String vhost) {
        List<String> listTopicPermissions = Lists.newArrayList();
        listTopicPermissions.add("list_topic_permissions");
        listTopicPermissions.add("-p");
        listTopicPermissions.add(vhost);
        return run(listTopicPermissions);
    }

    public ExecTools.ExecResult listUserPermissions(String username) {
        List<String> listUserPermissions = Lists.newArrayList();
        listUserPermissions.add("list_user_permissions");
        listUserPermissions.add(username);
        return run(listUserPermissions);
    }


    public ExecTools.ExecResult listUserTopicPermissions(String username) {
        List<String> listUserTopicPermissions = Lists.newArrayList();
        listUserTopicPermissions.add("list_user_topic_permissions");
        listUserTopicPermissions.add(username);
        return run(listUserTopicPermissions);
    }


}
